<?php
/**
 * User: hjd-yao
 * Date: 2019/6/14
 * Time: 11:34
 */

namespace Shop\Http\Controllers\Wap;


use App\Helpers\SelfEncrypt;
use App\Helpers\SelfResponse;
use App\Models\Lottery\PrizeModel;
use App\Models\Lottery\WinModel;
use App\Models\Shop\OrderGoodsModel;
use App\Models\Shop\OrderModel;

use App\Models\Shop\OrderPaymentModel;
use App\Models\Shop\ShopCartModel;
use App\Models\Shop\ShopGoodsModel;
use App\Models\Shop\ShopGoodsSpecValueModel;
use App\Models\Shop\UserAddressModel;
use Carbon\Carbon;
use DC\UserCenter\Models\UserAuthModel;
use DC\UserCenter\Models\UserBaseModel;
use DC\UserCenter\Models\UserWalletModel;
use Illuminate\Http\Request;
use PhpParser\Node\Stmt\Return_;
use Shop\Http\Controllers\BaseController;

use App\Models\System\ConfigModel;
use Yansongda\Pay\Pay;

class LotteryController extends BaseController
{
    public function goldEgg(Request $request){
        $user_id = $request->input('user_id');
        $can_cj_times=0;
        if($user_id){
            $user= UserBaseModel::where('member_id',$user_id)->first();
            $can_cj_times=$user->can_cj_times;
        }
        $prizes=PrizeModel::where('status',1)->get();

        $mask_rule=config("website.cj_rule");

        return $this->wapRender('lottery.gold_egg',[
            'page_title'=>'砸金蛋',
            'can_cj_times'=>$can_cj_times,
            'mask_rule'=>$mask_rule,
            'prizes'=>$prizes,
        ]);
    }

    /**
     * 我的中奖记录
     */
    public function mywin(Request $request){
        $user_id = $request->input('user_id');
        $wins=WinModel::where('user_id',$user_id)->where('status','>',0)->get();
        return $this->wapRender('lottery.mywin',[
            'page_title'=>'中奖记录',
            'list'=>$wins,
        ]);
    }

    /**
     * 砸金蛋
     * @param Request $request
     */
    public function zan(Request $request)
    {
        if($request->isMethod('post')){
            $user_id = $request->input('user_id');
            $user= UserBaseModel::where('member_id',$user_id)->first();
            $can_cj_times=$user->can_cj_times;
            if(!$can_cj_times>0){
                return SelfResponse::error('您没有抽奖次数');
            }
            $address = UserAddressModel::where('user_id',$user_id)->where('is_default',1)->first();
            if(!$address){
                return SelfResponse::error('请设置默认收货地址','f_Wap_Address_myAddress');
            }

            $prize_arr=PrizeModel::where('status',1)->pluck('power','id')->toArray();
            if(!$prize_arr){
                return SelfResponse::error('未上传奖品');
            }
            $pid = $this->get_rand($prize_arr); //根据概率获取奖项id
            $prize=PrizeModel::find($pid);
            //增加中奖记录
            $win_table=new WinModel();
            $win_table->title=$prize->title;
            $win_table->cover=$prize->cover;
            $win_table->status=1;
            $win_table->user_id=$user_id;
            $win_table->post_address=get_address($address->area).$address->address;
            $win_table->consignee=$address->consignee;
            $win_table->phone=$address->phone;
            $win_table->save();
            //减少次数
            UserBaseModel::where('member_id',$user_id)->decrement('can_cj_times');
            return SelfResponse::success('抽奖成功');
        }else{
            return SelfResponse::error('参数错误');
        }

    }

    // 抽奖概率计算
    public function get_rand($proArr)
    {
        $result = '';

        //概率数组的总概率精度
        $proSum = array_sum($proArr);

        //概率数组循环
        foreach ($proArr as $key => $proCur) {
            $randNum = mt_rand(1, $proSum);
            if ($randNum <= $proCur) {
                $result = $key;
                break;
            } else {
                $proSum -= $proCur;
            }
        }
        unset ($proArr);
        return $result;
    }

}